#include <iostream>
#include <cassert>
#include <vector>

using namespace std;

// 209. Minimum Size Subarray Sum
// https://leetcode.com/problems/minimum-size-subarray-sum/description/
//
// 暴力解法
// 该方法在 Leetcode 中会超时！
// 时间复杂度: O(n^3)
// 空间复杂度: O(1)
class Solution
{
public:
    int minSubArrayLen(int target, vector<int> &nums)
    {
        assert(target > 0);

        int res = nums.size() + 1; // 这个结果是不可能的，初始化
        for (int l = 0; l < nums.size(); l++)
            for (int r = l; r < nums.size(); r++)
            {
                int sum = 0;
                for (int i = l; i <= r; i++)
                    sum += nums[i];
                if (sum >= target)
                    res = min(res, r - l + 1); // 因为是在[l...r]的闭区间中，所以要+1
            }

        //说明没有找到结果
        if (res == nums.size() + 1)
            return 0;

        return res;
    }
};

int main()
{
    int nums[] = {2, 3, 1, 2, 4, 3};
    vector<int> vec = vector<int>(nums, nums + sizeof(nums) / sizeof(int));
    int s = 7;

    int res = Solution().minSubArrayLen(s, vec);
    cout << res << endl;

    return 0;
}